package com.example.a2302fx.fg.minefg

import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddEntity
import com.example.base.entity.OrderEntity
import com.example.base.mvvm.BaseViewModel
import com.example.base.mvvm.Res
import com.example.base.net.SingleLiveData
import kotlinx.coroutines.launch

class OrderViewModel:BaseViewModel() {

    val orderSuccess = SingleLiveData<Res<OrderEntity>>()
    val orderFail = SingleLiveData<String>()
    fun order(type: String,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                orderSuccess.postValue(apiService.order(type, limit, page))
            }catch (e:Exception){
                orderFail.postValue(e.message.toString())
            }
        }
    }

    val cancelSuccess = SingleLiveData<Res<String?>>()
    val cancelFail = SingleLiveData<String?>()
    fun cancel(id:Int){
        viewModelScope.launch {
            try {
                cancelSuccess.postValue(apiService.cancel(id))
            }catch (e:Exception){
                cancelFail.postValue(e.message)
            }
        }
    }

    val addSuccess = SingleLiveData<Res<AddEntity>>()
    val addFail = SingleLiveData<String?>()
    fun add(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.postValue(apiService.add(mapToBody(map)))
            }catch (e:Exception){
                addFail.postValue(e.message)
            }
        }
    }

    val refundSuccess = SingleLiveData<Res<String?>>()
    fun refund(id:Int,refund_reason_wap_explain:String,refund_reason_wap_img:String,text:String,uni:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["id"] = id
                map["refund_reason_wap_explain"] = refund_reason_wap_explain
                map["refund_reason_wap_img"] = refund_reason_wap_img
                map["text"] = text
                map["uni"] = uni
                refundSuccess.postValue(apiService.refund(mapToBody(map)))
            }catch (e:Exception){

            }
        }
    }
}